package io.vov.vitamio.db;

import io.vov.vitamio.TVApplication;
import io.vov.vitamio.entity.OnlineVideo;
import io.vov.vitamio.entity.OnlineVideoSource;
import io.vov.vitamio.entity.TVProgram;

import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.ndjh.ormlite.NDJHBaseDao;
import com.ndjh.ormlite.NDJHBaseDaoImpl;

/**
 * Database helper which creates and upgrades the database and provides the DAOs
 * for the app.
 * 
 * @author X-ray
 * @param <T>
 */
public class DatabaseHelper<T> extends OrmLiteSqliteOpenHelper {

	/***************************************************************************
	 * Suggested Copy/Paste code. Everything from here to the done block.
	 **************************************************************************/

	private static final String DATABASE_NAME = "tv.db";
	private static final int DATABASE_VERSION = 2;
	private NDJHBaseDao<T, Integer> TVDao;
	private ArrayList<OnlineVideo> OnlineVideoList = new ArrayList<OnlineVideo>();

	/**
	 * 初始化数据库操作
	 * 
	 * @param context
	 */
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/***************************************************************************
	 * Suggested Copy/Paste Done
	 **************************************************************************/

	/**
	 * 初始化数据库，这里可以调用TableUtils.createTable 初始化表结构，以及初始化数据操作
	 */
	@Override
	public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, OnlineVideo.class);
			TableUtils.createTable(connectionSource, OnlineVideoSource.class);
			TableUtils.createTable(connectionSource, TVProgram.class);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e);
		}
	}

	/**
	 * 数据库升级操作，这里你可以实现数据迁移工作
	 */
	@Override
	public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) {
		try {
			Log.e("电视直播","数据库升级");
			sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + " temp_"+ OnlineVideo.class.getSimpleName());
			sqliteDatabase.execSQL("ALTER TABLE "+ OnlineVideo.class.getSimpleName()+" RENAME TO " +" temp_"+ OnlineVideo.class.getSimpleName());
			System.err.println("onUpgrade.oldVer: " + oldVer + " newVer: " + newVer);
			TableUtils.dropTable(connectionSource, OnlineVideoSource.class, true);
			TableUtils.dropTable(connectionSource, OnlineVideo.class, true);
			TableUtils.dropTable(connectionSource, TVProgram.class, true);
			onCreate(sqliteDatabase, connectionSource);
			
			Cursor mCursor  = sqliteDatabase.rawQuery("select * from temp_onlineVideo where isFavorite = 1;", null);
			
			while(mCursor!=null && mCursor.moveToNext()){
				OnlineVideo OnlineVideodata = new OnlineVideo();
				int nameFieldColumnIndex = mCursor.getColumnIndex("title");
				String title = mCursor.getString(nameFieldColumnIndex);
				OnlineVideodata.setTitle(title);
				OnlineVideoList.add(OnlineVideodata);
				Log.e(DATABASE_NAME,"mCursor============end"+title);
			}
			Log.e("DATABASE_NAME","mCursor============getCount()"+mCursor.getCount());
			mCursor.close();
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new " + newVer, e);
		}
	}

	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		try {
			System.err.println("onDowngrade.oldVer: " + oldVersion + " newVer: " + newVersion);
			db.execSQL("DROP TABLE IF EXISTS " + OnlineVideoSource.class.getSimpleName());
			db.execSQL("DROP TABLE IF EXISTS " + OnlineVideo.class.getSimpleName());
			db.execSQL("DROP TABLE IF EXISTS " + TVProgram.class.getSimpleName());
			onCreate(db);
		} catch (Exception e) {
			Log.e(DatabaseHelper.class.getName(), "Unable to downgrade database from version " + oldVersion + " to new " + newVersion, e);
		}
	}

	public NDJHBaseDao<T,Integer> getDao(Class<T> clazz) throws SQLException {
		TVDao = NDJHBaseDaoImpl.createDao(getConnectionSource(), clazz);
		return TVDao;
	}
	
	public ArrayList<OnlineVideo> getOldFavorite() {
		
		return OnlineVideoList;
		
	}

}
